package com.jacobson.core.di.modules

import android.app.Application
import com.google.gson.Gson
import com.jacobson.core.framework.PrettyHttpLoggingInterceptorProxy
import com.jacobson.core.framework.configuration.OkHttpClientConfiguration
import com.jacobson.core.framework.configuration.RetrofitConfiguration
import com.jacobson.core.framework.errorhandler.OnErrorListener
import com.jacobson.core.framework.errorhandler.RxErrorHandler
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

/**
 * ================================================
 * 提供一些三方客户端实例
 * @author Wucz on 2018/7/18 15:27
 * ================================================
 */

@Module
abstract class ClientModule {
    @Module
    companion object {
        /**
         * 提供全局Retrofit实例
         */
        @Singleton
        @Provides
        @JvmStatic
        fun provideRetrofit(application: Application, configuration: RetrofitConfiguration?,
                            client: OkHttpClient, gson: Gson): Retrofit {
            Retrofit.Builder().run {
                configuration?.let {
                    it.baseUrl?.let { baseUrl(it) }
                    it.onBuildInterceptor?.onBuildIntercept(application, this)
                }
                client(client)
                addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                addConverterFactory(GsonConverterFactory.create(gson))
                return build()
            }
        }

        @Singleton
        @Provides
        @JvmStatic
        fun provideOkHttpClient(application: Application, configuration: OkHttpClientConfiguration?): OkHttpClient {
            OkHttpClient.Builder().run {
                configuration?.let { it ->
                    //设置Connect Timeout
                    connectTimeout(it.connectTimeout, TimeUnit.MILLISECONDS)
                    //设置Read Timeout
                    readTimeout(it.readTimeout, TimeUnit.MILLISECONDS)
                    //全局Http发送前拦截
                    it.globalHttpHandler.run {
                        addInterceptor { chain -> chain.proceed(onHttpRequestBefore(chain, chain.request())) }
                    }
                    //添加外部的拦截器集合
                    it.interceptors.forEach {
                        addInterceptor(it)
                    }
                    it.onBuildInterceptor?.onBuildIntercept(application, this)

                    val loggingInterceptor = HttpLoggingInterceptor(PrettyHttpLoggingInterceptorProxy())
                    loggingInterceptor.level = it.httpLoggerLevel
                    addInterceptor(loggingInterceptor)
                }
                return build()
            }
        }

        @Singleton
        @Provides
        @JvmStatic
        fun provideRxErrorHandler(context: Application, listener: OnErrorListener) =
                RxErrorHandler.Builder().context(context).onErrorListener(listener).build()

    }
}